
$(function() {
		
	$("input[id^='code_s']:visible").autocomplete('info.php?do=Equipment.ajax_code', {
		width: 200,
		max: 20,
		dataType: "json",
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.name,
					result: row.code
				}
			});
		},
		formatItem: function(item) {
			return item.name + " &lt;" + item.code + "&gt;";
		}
	});

	$("select[id^='category_']:visible").selectChain({
		target: $("select[id^='type_']:visible"),
		key: "code",
		value: "name",
		url: 'info.php?do=Equipment.ajax_category',
		data: { ajax: true }
	}).trigger('change');
	
	Tip.emp('负责人');
	
});

var Equipment = {

	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.code = form.code.value;
				body.name = form.name.value;
				
				body.category = form.category.value;
				body.type = form.type.value;

				body.purpose = form.purpose.value;
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('info.php?do=Equipment.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_cal_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"计算": function() {
				if (!confirm("确认是否开始计算？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				body.emp = form.emp.value;
				body.t1 = form.t1.value;
				body.t2 = form.t2.value;

				body.content = form.content.value;

				$(this).dialog("close");

				$.post('info.php?do=Equipment.calPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	clean_cache: function () {
		$.getJSON('info.php?do=Equipment.audit&item=52', function(json) {
			if (json.status == 1) {
				if (!confirm("确认是否清除缓存？")) {
					return false;
				}

				$.get('info.php?do=Equipment.ajax_category_type', function(data) {
					alert("操作成功！");
				});
			} else {			
				_error();
			}
		});
	},	

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		$.cookie('status_', status);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'info.php?do=Equipment.edit&id=' + id;	
		var c2 = 'info.php?do=Equipment.view&id=' + id;
		var c3 = 'info.php?do=Equipment.copy&id=' + id;

		var a1 = ['编辑信息', 'url', 'Equipment.dialog_edit_', '600x400'];
		var a2 = ['查看信息', 'url', 'Equipment.dialog_view_', '600x400'];
		var a3 = ['复制信息', 'url', 'Equipment.dialog_copy_', '600x400'];
				
		// 闲置未用
		if (status == 1) {
			var d1 = 'info.php?do=Equipment.emp&id=' + id;	
			
			var b1 = ['分配使用', 'url', 'Equipment.dialog_emp_', '650x420'];
			var s = c1 + ", " + c2 + ", " + c3 + ", " + d1;

			return this.popup_(e, [a1, a2, a3, b1], s);
		}

		// 正在使用
		if (status == 2) {
			var d1 = 'info.php?do=Equipment.idle&id=' + id;
			var d2 = 'info.php?do=Equipment.discard&id=' + id;
			
			var b1 = ['资产空闲', 'url', 'Equipment.list_idle'];
			var b2 = ['资产报废', 'url', 'Equipment.dialog_discard', '650x400'];
			
			var s = c1 + ", " + c2 + ", " + c3 + ", " + d1 + ", " + d2;

			return this.popup_(e, [a1, a2, a3, b1, b2], s);
		}

		// 已经报废
		var s = c2;

		return this.popup_(e, [a2], s);	
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'info.php?do=Equipment.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Equipment.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('info.php?do=Equipment.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'info.php?do=Equipment.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.code = form.code.value;
				body.name = form.name.value;
				
				body.category = form.category.value;
				body.type = form.type.value;

				body.purpose = form.purpose.value;
				body.content = form.content.value;

				$(this).dialog("close");
				
				var url = 'info.php?do=Equipment.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},

	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.code = form.code.value;
				body.name = form.name.value;
				
				body.category = form.category.value;
				body.type = form.type.value;

				body.purpose = form.purpose.value;
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('info.php?do=Equipment.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_view_: function (url, title, xy) {
		var status = $.cookie('status_');

		if (status == 1) {	
			$('#dialog_view').dialog('option', 'buttons', {
				"关闭": function() { 
					$(this).dialog("close");
				},

				"分配使用人": function() {
					$(this).dialog("close");

					var url = 'info.php?do=Equipment.emp&id=' + $.cookie('id_'); 
					// alert(url);
					
					dialog_emp_(url, '分配使用人', '420x220');
				}
			});
		} else {
			$('#dialog_view').dialog('option', 'buttons', {

				"关闭": function() { 
					$(this).dialog("close");
				}
			});
		}

		dialog_view(url, title, xy);
	},

	dialog_emp_: function (url, title, xy) {	
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"分配": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var form = $("#form_edit")[0];

				var body = { };
				body.id = form.id.value;
				body.emp = form.emp.value;
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('info.php?do=Equipment.empPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	list_idle: function (url, view) {
		if (confirm("资产是否空闲？")) {
			list(url, view);
		}
	},

	/**
	 * 报废
	 */
	dialog_discard: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"提交": function() {
				if (!confirm("确认是否报废？")) {
					return false;
				}

				var form = $("#form_edit")[0];
				
				var body = { };
				body.id = form.id.value;
				body.content = form.content.value;

				$(this).dialog("close");

				var url = 'info.php?do=Equipment.discardPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	}
};
